package com.reesechou.controller.blog;

import com.reesechou.javabean.Article;
import com.reesechou.javabean.Pager;
import com.reesechou.javabean.User;
import com.reesechou.service.*;
import org.directwebremoting.util.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by 小星打屁屁 on 2015/4/27.
 * contact me ==>243709758@qq.com
 */
@Controller
@RequestMapping("/jsp/blog")
public class HomeController {

    private static Logger logger = Logger.getLogger(HomeController.class);

    private static final String HOME = "/jsp/blog/home";
    @Autowired
    private CategoryService categoryService ;

    @Autowired
    private TagService tagService ;

    @Autowired
    private CommentService commentService ;

    @Autowired
    private SiteConfigService siteConfigService ;

    @Autowired
    private LinkService linkService ;

    @Autowired
    private UserService userService ;

    @Autowired
    private ArticleService articleService ;
    /**
     * 这个类是用来处理 类型之间的转换的问题，如 date --> String 之间的转换
     * @param binder
     */
    @InitBinder
    private void dateBinder(WebDataBinder binder) {
        //The date format to parse or output your dates
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        //Create a new CustomDateEditor
        CustomDateEditor editor = new CustomDateEditor(dateFormat, true);
        //Register it as custom editor for the Date type
        binder.registerCustomEditor(Date.class, editor);
    }

    @RequestMapping(value="/home")
    public String home(Model model, HttpServletRequest request,Integer currentPage){
        if(currentPage == null) currentPage = 1;

        createReferenceData(model,currentPage,request);
        return HOME;
    }

    /**
     * 保存相关主页面的信息
     * @param model
     * @param currentPage
     * @param request
     *
     */
    private void createReferenceData(Model model,Integer currentPage,HttpServletRequest request) {
        User user = new User();
        User user1 = userService.find(user);
        model.addAttribute("user", userService.find(user));//默认是只有一个用户的，这个参数是没有起作用的
        model.addAttribute("tags", tagService.getAllTag());
        model.addAttribute("categorys", categoryService.findAllCategory());
        model.addAttribute("siteConfig", siteConfigService.findAllSiteConfig());//默认只有一个站点信息

        //分页的处理
        Pager pager = new Pager();
        List<Article> articles = articleService.getAllArticle();
        String url = request.getContextPath() + "/jsp/blog/page/";
        pager.setUrl(url);
        pager.doWithArticles(articles, currentPage, request);
        model.addAttribute("articles", articles);
        model.addAttribute("pager", pager);

        //TODO:最新的五篇文章
        model.addAttribute("newestArticle", articleService.findNewestArticle());

        //TODO:最新的五篇评论
        model.addAttribute("newestComment", commentService.findNewestComment());

        //TODO:最新的五个友情链接
        model.addAttribute("newestLink", linkService.findNewestLink());

        //这里是处理页面中的导航条的部分 对应的List 为 navItems , 类型值为 List<Article>
        List<Article> navItems = articleService.findPage();
        model.addAttribute("navItems", navItems);
    }
}
